<?php

class SanPhamController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		$model = Tbsanpham::model()->findAll();
		$this->render('index', array('model'=>$model));
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

	
	public function actionCreate()
	{
		$model = new Tbsanpham;
		
		if(isset($_POST['Tbsanpham']))
		{
			$model->attributes=$_POST['Tbsanpham'];	
			if(SanPhamdao::Insert($model) ){
				 $this->redirect(array('index'));
			}
		}else{
			// display the login form
			$var =Tbdonvitinh::model()->findAll();
			$data['donvitinh'] = CHtml::listData($var, "id", "tendonvi");
			$data['model'] = $model;
			$this->render('create',$data);
		}

	}
	
	public function actionTonKho($idkho)
	{
		$model = "";
		if(!isset($_REQUEST['idkho'])){
			$model = Tbtonkho::model()->findAll();
		}else{
			$model = TonKhodao::GetTonKho($idkho);
		}
		$khos = Tbkhohang::model()->findAll();
		$kho = new Tbkhohang;
		$data['list'] = $model;
		$data['khos'] = CMap::mergeArray(array(''=>'Tất cả'), CHtml::listData($khos, "id", "tenkho"));
		$data['kho'] = $kho;
		$this->render('tonkho', $data);
	}
}